import java.util.HashSet;

public class Solution202 {
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while (n != 1) {
            int t = 0;
            while (n != 0) {
                int digit = n % 10;
                t += digit * digit;
                n /= 10;
            }
            if (!set.add(t)) {
                return false;
            }
            n = t;
        }
        return true;
    }
}
